"""
File name: RandomColorMap.py
Input: n
Output: a tuple or list
Last modified: Apr 16, 2022
"""

import colorsys # for colorsys.hls_to_rgb
import random   # for random.random


class RandomColorMapList(object):
    """
    N colors different generated randomly
    input:  n
    output: A tuple or list including n elements
    """
    def __init__(self, n):
        self.num = n

    def get_colorMap(self):
        return tuple(map(lambda x: self.color(tuple(x)), self.ncolors()))

    def get_n_hls_colors(self):
        hls_colors = []
        i = 0
        step = 360.0 / self.num
        while i < 360:
            h = i
            s = 90 + random.random() * 10
            l = 50 + random.random() * 10
            _hlsc = [h / 360.0, l / 100.0, s / 100.0]
            hls_colors.append(_hlsc)
            i += step

        return hls_colors

    def ncolors(self):
        rgb_colors = []
        if self.num < 1:
            return rgb_colors
        hls_colors = self.get_n_hls_colors()
        for hlsc in hls_colors:
            _r, _g, _b = colorsys.hls_to_rgb(hlsc[0], hlsc[1], hlsc[2])
            r, g, b = [int(x * 255.0) for x in (_r, _g, _b)]
            rgb_colors.append([r, g, b])

        return rgb_colors

    def color(self, value):
        digit = list(map(str, range(10))) + list("ABCDEF")
        if isinstance(value, tuple):
            string = '#'
            for i in value:
                a1 = i // 16
                a2 = i % 16
                string += digit[a1] + digit[a2]
            return string
        elif isinstance(value, str):
            a1 = digit.index(value[1]) * 16 + digit.index(value[2])
            a2 = digit.index(value[3]) * 16 + digit.index(value[4])
            a3 = digit.index(value[5]) * 16 + digit.index(value[6])
            return (a1, a2, a3)